import 'dart:math';

import 'package:flutter/material.dart';
import 'package:xbxxz_assistant/data/area_map_data.dart';
import 'package:xbxxz_assistant/data/map_data.dart';

class StealMapPage extends StatefulWidget {
  @override
  _StealMapPageState createState() => _StealMapPageState();
}

class _StealMapPageState extends State<StealMapPage> {
  String _areaValue;
  String _mapValue;
  List<MapData> _mapDataList;

  static final TextEditingController _AttController = TextEditingController();
  static final TextEditingController _DefenseController =
      TextEditingController();
  static final TextEditingController _HPController = TextEditingController();
  static final TextEditingController _YAttController = TextEditingController();
  static final TextEditingController _GAttController = TextEditingController();

  // 妖王最多攻击次数
  int _yMaxA = 0;

  // 妖王最少攻击次数
  int _yMinA = 0;

  // 妖王最多扛几刀
  int _kY = 0;

  // 鬼王最多攻击次数
  int _gMaxA = 0;

  // 鬼王最少攻击次数
  int _gMinA = 0;

  // 鬼王最多扛几刀
  int _kG = 0;

  // 提升建议
  // 妖王最少攻击次数
  int _upYMinA = 0;

  // 妖王攻击提升
  int _upYA = 0;

  // 妖王防御提升
  int _upYD = 0;

  // 妖王生命提升
  int _upYHP = 0;

  // 鬼王最少攻击次数
  int _upGMinA = 0;

  // 鬼王攻击提升
  int _upGA = 0;

  // 鬼王防御提升
  int _upGD = 0;

  // 鬼王生命提升
  int _upGHP = 0;

  @override
  void initState() {
    super.initState();
    setState(() {
      _areaValue = "heaven";
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: <Widget>[
              buildAreaDropDown(),
              Divider(height: 3),
              buildMapDropDown(),
              Divider(height: 3),
              buildAttributeInput(),
              Divider(height: 3),
              buildCalculator(),
              Divider(height: 3),
              buildUpSuggest(),
              buildMap(),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildMap() {
    if (_mapValue == null) {
      return Container();
    }
    return Center(child: Image.asset("images/$_areaValue/$_mapValue.jpg"));
  }

  Widget buildUpSuggest() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.brown, width: 0.5),
      ),
      child: Column(
        children: <Widget>[
          Center(child: Text("提升建议")),
          Row(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Column(
                  children: <Widget>[
                    Text("妖王"),
                    Text("提升$_upYMinA攻击可以少砍一刀(暴击)"),
                    Text("提升$_upYA攻击可以少砍一刀"),
                    Text("提升$_upYD防御可以多扛一刀"),
                    Text("提升$_upYHP生命可以多扛一刀"),
                  ],
                ),
              ),
              Expanded(
                flex: 1,
                child: Column(
                  children: <Widget>[
                    Text("鬼王"),
                    Text("提升$_upGMinA攻击可以少砍一刀(暴击)"),
                    Text("提升$_upGA攻击可以少砍一刀"),
                    Text("提升$_upGD防御可以多扛一刀"),
                    Text("提升$_upGHP生命可以多扛一刀"),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildCalculator() {
    return Column(
      children: <Widget>[
        FlatButton(
          child: Center(child: Text("计算")),
          textColor: Colors.black,
          color: Colors.brown,
          shape: RoundedRectangleBorder(
            side: BorderSide(
              color: Colors.brown,
              width: 1,
            ),
            borderRadius: BorderRadius.circular(8),
          ),
          onPressed: () {
            calculator();
          },
        ),
        Container(
          decoration: BoxDecoration(
            border: Border.all(color: Colors.brown, width: 0.5),
          ),
          child: Row(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: buildY(),
              ),
              Expanded(
                flex: 1,
                child: buildG(),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget buildY() {
    return Center(
      child: Column(
        children: <Widget>[
          Text("妖王"),
          Text("可以扛$_kY刀不死"),
          Text("最少攻击$_yMinA次"),
          Text("最多攻击$_yMaxA次"),
        ],
      ),
    );
  }

  Widget buildG() {
    if (_kG < 0) {
      return Center(
        child: Column(
          children: <Widget>[
            Text("鬼王"),
            Text("不破防"),
          ],
        ),
      );
    } else {
      return Center(
        child: Column(
          children: <Widget>[
            Text("鬼王"),
            Text("可以扛$_kG刀不死"),
            Text("最少攻击$_gMinA次"),
            Text("最多攻击$_gMaxA次"),
          ],
        ),
      );
    }
  }

  void calculator() {
    int pAtt = int.parse(
        _AttController.text == null || _AttController.text == ""
            ? "0"
            : _AttController.text);
    int pDef = int.parse(
        _DefenseController.text == null || _DefenseController.text == ""
            ? "0"
            : _DefenseController.text);
    int pHP = int.parse(_HPController.text == null || _HPController.text == ""
        ? "0"
        : _HPController.text);
    int yAtt = int.parse(
        _YAttController.text == null || _YAttController.text == ""
            ? "0"
            : _YAttController.text);
    int gAtt = int.parse(
        _GAttController.text == null || _GAttController.text == ""
            ? "0"
            : _GAttController.text);
    if (pAtt == null ||
        pAtt == 0 ||
        pDef == null ||
        pDef == 0 ||
        pHP == null ||
        pHP == 0) {
      return;
    }
    for (var mapData in _mapDataList) {
      if (mapData.name == _mapValue) {
        int hp = int.parse(mapData.hp);
        int yA = int.parse(mapData.yatt);
        int gA = int.parse(mapData.gatt);
        // 妖王最多攻击次数
        int yMaxA = hp ~/ (pAtt + yAtt);
        // 妖王最少攻击次数
        int yMinA = yMaxA ~/ 2;
        // 妖王最多扛几刀
        int kY = pHP ~/ (yA - pDef);
        // 鬼王最多攻击次数
        int gMaxA = hp ~/ (pAtt + gAtt);
        // 鬼王最少攻击次数
        int gMinA = gMaxA ~/ 2;
        // 鬼王最多扛几刀
        int kG = pHP ~/ (gA - pDef);
        setState(() {
          _yMaxA = yMaxA;
          _yMinA = yMinA;
          _kY = kY;
          _gMaxA = gMaxA;
          _gMinA = gMinA;
          _kG = kG;
        });
        // 提升建议
        if (kY >= 0) {
          // 妖王最少攻击次数
          int upYMinA =
              max((hp / 2 / max((yMinA.floor()), 1)).ceil() - (pAtt + yAtt), 0);
          // 妖王攻击提升
          int upYA =
              max((hp / max(yMaxA.floor(), 1)).ceil() - (pAtt + yAtt), 0);
          // 妖王防御提升
          int upYD = max((yA - pHP / (kY + 1)).ceil() - pDef, 1);
          // 妖王生命提升
          int upYHP = max((kY + 1) * (yA - pDef) - pHP, 1);
          setState(() {
            _upYMinA = upYMinA;
            _upYA = upYA;
            _upYD = upYD;
            _upYHP = upYHP;
          });
        }
        if (kG >= 0) {
          // 鬼王最少攻击次数
          int upGMinA =
              max((hp / 2 / max((yMinA.floor()), 1)).ceil() - (pAtt + yAtt), 0);
          // 鬼王攻击提升
          int upGA = max(
              (hp / (max(yMaxA.floor(), 1)).ceil()).ceil() - (pAtt + yAtt), 0);
          // 鬼王防御提升
          int upGD = max((gA - (pHP / (kG + 1))).ceil() - pDef, 1);
          // 鬼王生命提升
          int upGHP = max((kG + 1) * (gA - pDef) - pHP, 1);
          setState(() {
            _upGMinA = upGMinA;
            _upGA = upGA;
            _upGD = upGD;
            _upGHP = upGHP;
          });
        }
      }
    }
  }

  Widget buildAttributeInput() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.brown, width: 0.5),
      ),
      child: Row(
        children: <Widget>[
          Expanded(
              flex: 1,
              child: Center(
                child: Text("数据"),
              )),
          Expanded(
            flex: 2,
            child: Column(
              children: <Widget>[
                TextField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    contentPadding: EdgeInsets.all(10.0),
                    labelText: '攻击',
                  ),
                  keyboardType: TextInputType.number,
                  controller: _AttController,
                ),
                Divider(),
                TextField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    contentPadding: EdgeInsets.all(10.0),
                    labelText: '防御',
                  ),
                  keyboardType: TextInputType.number,
                  controller: _DefenseController,
                ),
                Divider(),
                TextField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    contentPadding: EdgeInsets.all(10.0),
                    labelText: '生命',
                  ),
                  keyboardType: TextInputType.number,
                  controller: _HPController,
                ),
                Divider(),
                TextField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    contentPadding: EdgeInsets.all(10.0),
                    labelText: '妖攻',
                  ),
                  keyboardType: TextInputType.number,
                  controller: _YAttController,
                ),
                Divider(),
                TextField(
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    contentPadding: EdgeInsets.all(10.0),
                    labelText: '鬼攻',
                  ),
                  keyboardType: TextInputType.number,
                  controller: _GAttController,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildMapDropDown() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.brown, width: 0.5),
      ),
      child: Row(
        children: <Widget>[
          Expanded(
              flex: 1,
              child: Center(
                child: Text("地图"),
              )),
          Expanded(
            flex: 2,
            child: DropdownButton(
              isExpanded: true,
              icon: Icon(Icons.expand_more),
              items: _getMapData(),
              value: _mapValue,
              onChanged: (T) {
                setState(() {
                  _mapValue = T;
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget buildAreaDropDown() {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.brown, width: 0.5),
      ),
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Center(
              child: Text("地区"),
            ),
          ),
          Expanded(
            flex: 2,
            child: DropdownButton(
              isExpanded: true,
              icon: Icon(Icons.expand_more),
              items: _getAreaData(),
              value: _areaValue,
              onChanged: (T) {
                setState(() {
                  _areaValue = T;
                  _mapValue = null;
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  List<DropdownMenuItem> _getAreaData() {
    List<DropdownMenuItem> items = List();
    for (var item in areaMapData["areas"]) {
      DropdownMenuItem dropdownMenuItem = DropdownMenuItem(
        child: Text(item["name"]),
        value: item["value"],
      );
      items.add(dropdownMenuItem);
    }

    return items;
  }

  void resetCalculator() {
    setState(() {
      _yMaxA = 0;
      _yMinA = 0;
      _kY = 0;
      _gMaxA = 0;
      _gMinA = 0;
      _kG = 0;
      _upYMinA = 0;
      _upYA = 0;
      _upYD = 0;
      _upYHP = 0;
      _upGMinA = 0;
      _upGA = 0;
      _upGD = 0;
      _upGHP = 0;
    });
  }

  List<DropdownMenuItem> _getMapData() {
    List<DropdownMenuItem> items = List();
    Map<String, List<dynamic>> map = areaMapData["maps"];
    List<MapData> dataList = List();
    for (var item in map[_areaValue]) {
      MapData data = MapData.fromJson(item);
      DropdownMenuItem dropdownMenuItem = DropdownMenuItem(
        child: Text(item["name"]),
        value: data.name,
      );
      items.add(dropdownMenuItem);
      dataList.add(data);
    }
    setState(() {
      _mapDataList = dataList;
    });
    return items;
  }
}
